<template>
    <div class="reg">
        <van-nav-bar title="注册" left-arrow @click-left="$router.back()" />
        <div class="main">
            <svg class="icon tt-icon" aria-hidden="true">
                <use xlink:href="#icon-yonghu"></use>
            </svg>
            <van-form @submit="onSubmit" ref="regForm">
                <van-field v-model="username" name="username" placeholder="请输入手机号" :rules="[
                    { required: true, },
                    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' },
                ]">
                    <template #left-icon>
                        <svg class="icon tel" aria-hidden="true">
                            <use xlink:href="#icon-shouji"></use>
                        </svg>
                    </template>
                </van-field>

                <van-field v-model="vercode" name="vercode" center clearable placeholder="请输入短信验证码" :rules="[
                    { required: true,  },
                    {
                    pattern: /^\d{6}$/,
                    message: '验证码格式错误',
                    },
                ]">
                    <template #left-icon>
                        <svg class="icon tel" aria-hidden="true">
                            <use xlink:href="#icon-yanzhengyanzhengma"></use>
                        </svg>
                    </template>
                    <template #button>
                        <van-button size="small" round color="#5E81B5" native-type="button" @click="send"
                            :disabled="btnDisabled">{{ code_text }}
                        </van-button>
                    </template>
                </van-field>


                <van-field v-model="password" type="password" name="password" placeholder="请输入密码" :rules="[
                    { required: true },
                    {
                    validator: (e) => /^\w{6,12}$/.test(e),
                    message: '密码格式为6-12位字母或数字',
                    },
                ]">
                    <template #left-icon>
                        <svg class="icon pwd" aria-hidden="true">
                            <use xlink:href="#icon-mima"></use>
                        </svg>
                    </template>
                </van-field>
                <div class="footer">
                    <van-checkbox checked-color="#5E81B5" v-model="checked">请同意用户协议</van-checkbox>
                </div>
                <div style="margin: 16px;">
                    <van-button type="primary" native-type="submit" round block :loading="submitLoading">点击注册
                    </van-button>
                    <!-- <van-button round block class="button" type="info" >提交</van-button> -->
                </div>
            </van-form>
        </div>

    </div>
</template>

<script>
import { Field, CellGroup, Button, Checkbox, CheckboxGroup, Form } from 'vant';
import { reg, sendSms } from '@/API/user';
export default {
    name: 'reg',
    data() {
        return {
            username: '',
            password: '',
            vercode: '',
            newPassword: '',
            checked: false,
            code_text: "发送验证码",
            btnDisabled: false,
            submitLoading:false,
        }
    },
    components: {
        [Field.name]: Field,
        [CellGroup.name]: CellGroup,
        [Button.name]: Button,
        [Checkbox.name]: Checkbox,
        [CheckboxGroup.name]: CheckboxGroup,
        [Form.name]: Form,
    },
    methods: {
        // 发送验证码
        async send() {
            try {
                await this.$refs.regForm.validate("username");
                //    发送验证码按钮点击进入倒计时
                this.btnDisabled = true; //变成不可点击状态
                this.code_text = "发送中...";
                // 定义一个定时器
                let timer = null;
                // 定义一个倒计时的时间
                let count = 60;

                sendSms({
                    mobile: this.username,
                    type: "register",
                })
                    .then((res) => {
                        if (res.code == 0) {
                            this.$toast(`验证码已经发送到${this.username},请注意查收`);
                            console.log(res.data);
                            timer = setInterval((e) => {
                                count--;
                                this.code_text = `${count}s后再次发送`;
                                if (count <= 0) {
                                    clearInterval(timer);
                                    count = 60;
                                    this.code_text = "再次获取";
                                    this.btnDisabled = false;
                                }
                            }, 1000);
                        } else {
                            // 例如账号已存在等情况
                            this.$toast(res.msg);
                            this.btnDisabled = false; //变成不可点击状态
                            this.code_text = "再次发送";
                        }

                        //   例如网络错误等原因进入到下面这个catch  并且提示他发送失败了 并进行再次发送  并且恢复到可点击状态
                    })
                    .catch((err) => {
                        this.$toast(res.msg || "网络错误");
                        this.btnDisabled = false; //变成不可点击状态
                        this.code_text = "请再次请求";
                    });
            } catch (error) { }
        },
        onSubmit(values) {
            console.log('submit', values);
            this.submitLoading = true;
            reg({
                values
            }).then(res => {
                console.log(res);
                this.$toast(res.msg)
                his.submitLoading = false;
            },
                err => {
                    this.$toast(err.msg)
                    his.submitLoading = false;
                })
        },
    },

}
</script>

<style lang="less" scoped>
.reg {
    width: 100vw;
    height: 100vh;
    position: relative;
    background-color: @baseColor;

    .main {

        box-sizing: border-box;
        padding: 20px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: 80%;
        height: 65%;
        border-radius: 18px;
        background-color: rgba(255, 255, 255, .5);
        text-align: center;

        .tt-icon {
            font-size: 50px;
        }

        .tel {
            font-size: 24px;
        }

        .pwd {
            font-size: 24px;
        }

        .field {
            margin-top: 10.6667vw;
        }

        .button {
            margin-top: 10px;
        }

        .footer {
            width: 260px;
            padding: 10px 0;
        }

    }
}

::v-deep .van-cell {
    background-color: rgba(255, 255, 255, .0);
}

::v-deep .van-cell-group {
    background-color: rgba(255, 255, 255, .0);
}

::v-deep .van-nav-bar {
    background-color: rgba(255, 255, 255, .0);
}

::v-deep .van-hairline--bottom::after {
    border: none;
}

::v-deep .van-nav-bar .van-icon {
    color: #fff;
}

::v-deep .van-nav-bar__title {
    color: #fff;
}

::v-deep .van-checkbox__label {
    color: #fff;
}

::v-deep .van-checkbox {
    justify-content: center;
}
</style>